<template>
  <view class="settle-container">
    <label class="radio">
      <radio color="#C00000" :checked="isFullCheck" @click="selectAllOrNot"/><text>全选</text>
    </label>
    <view class="amount">
      合计:<text class="price">￥{{GoodsAmountPrice}}</text>
    </view>
    <view class="settle" @click="settlement">结算({{checkedCount}})</view>
  </view>
</template>

<script>
  import {mapGetters, mapMutations, mapState} from 'vuex'
  export default {
    name:"Settle",
    data() {
      return {
        
      };
    },
    computed: {
      ...mapGetters("cartAbout", ['checkedCount', 'total', 'GoodsAmountPrice']),
      ...mapGetters("addressAbout", ['addStr']),
      ...mapState("userAbout", ['token']),
      ...mapState('cartAbout', ['cart']),
      // 是否全选
      isFullCheck() {
        return this.checkedCount === this.total
      }
    },
    methods: {
      ...mapMutations("cartAbout", ['SELECTALLORNOT']),
      // 全选or取消全选
      selectAllOrNot() {
        this.SELECTALLORNOT(!this.isFullCheck)
      },
      // 结算
      settlement() {
        // 未登录
        if (!this.token) return uni.showModal({
            content: "请先登录",
            confirmText: "去登录",
            success(res) {
              if (res.confirm) {
                uni.switchTab({
                  url: "/pages/my/my"
                })
              }
            }
          })
        // 未勾选
        if (!this.checkedCount) return uni.$showMsg("请选择要结算的商品")
        // 未选择收货地址
        if (!this.addStr) return uni.$showMsg("请选择收货地址")
        // 订单
        this.payOrder()
      },
      // 创建订单
      async payOrder() {
        const good = this.cart.filter(item => item.goods_state)
        const goods = good.map(item => ({
          goods_id: item.goods_id,
          goods_number: item.goods_count,
          goods_price: item.goods_price
        }))
        const orderInfo = {
          order_price: 0.01,
          consignee_addr: this.addStr,
          goods: goods
        }
        // 发起订单请求
        const result = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
        // 由于token是假的,所以也不能获取订单号,自己创建一个假的
        if (result.data.meta.status !== 200) return uni.$showMsg('订单生成失败')
        // const orderNumber  = result.data.message.order_number
        const orderNumber  = "GD20180504000000000045"
        // // 订单预支付
        const res = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {order_number: orderNumber})
        if (res.data.meta.status !== 200) return uni.$showMsg('预订单生成失败')
        const payInfo = res.data.message.pay
        // 发起微信支付
        // 调用 uni.requestPayment() 发起微信支付
        const [err, succ] = await uni.requestPayment(payInfo)
        // 3.2 未完成支付
        if (err) return uni.$showMsg('订单未支付！')
        // 3.3 完成了支付，进一步查询支付的结果
        const { data: res3 } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', { order_number: orderNumber })
        // 3.4 检测到订单未支付
        if (res3.meta.status !== 200) return uni.$showMsg('订单未支付！')
        // 3.5 检测到订单支付完成
        uni.showToast({
          title: '支付完成！',
          icon: 'success'
        })
      }
    }
  }
</script>

<style lang="scss">
  .settle-container {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px;
    background-color: #FFF;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    .radio {
      padding: 5px;
    }
    .amount {
      .price {
        color: #C00000;
        font-weight: 700;
      }
    }
    .settle {
      height: 100%;
      font-size: 16px;
      min-width: 100px;
      line-height: 50px;
      text-align: center;
      color: #FFF;
      padding: 0 10px;
      background-color: #C00000;
    }
  }
</style>